home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / misc / executiv.lha / Executive_V1.00 / sysinfo.lzx / examples / test / test.c < prev   
C/C++ Source or Header  |  1990-09-28  |  8KB  |  294 lines

  1. /*
  2.  * Test-program for some sysinfo.library features
  3.  *
  4.  * This file is public domain.
  5.  *
  6.  * Author: Petri Nordlund <petrin@mits.mdata.fi>
  7.  *
  8.  * $Id: test.c 1.2 1995/07/27 12:28:02 petrin Exp petrin $
  9.  *
  10.  */
  11.  
  12. #include "defs.h"
  13. #include <proto/sysinfo.h>
  14. #include <libraries/sysinfo.h>
  15.  
  16. /* VARIABLES */
  17. struct sysinfo *si = NULL;
  18. struct Library *SysinfoBase = NULL;
  19.  
  20.  
  21. int
  22. main(int argc, char **argv)
  23. {
  24.     if(!(SysinfoBase = OpenLibrary(SYSINFONAME, SYSINFOVERSION)))
  25.     {
  26.         puts("Can't open sysinfo.library");
  27.         exit(RETURN_FAIL);
  28.     }
  29.  
  30.  
  31.     /* Initialize the sysinfo.library, this will make connection to the
  32.      * server-process and allocate the sysinfo-structure. */
  33.     if(!(si = InitSysinfo()))
  34.     {
  35.         puts("Couldn't initialize sysinfo");
  36.         CloseLibrary(SysinfoBase);
  37.         exit(RETURN_FAIL);
  38.     }
  39.  
  40.  
  41.     /* print our pid, ppid and pgrp */
  42.     printf("pid:  %d\n",GetPid(si));
  43.     if(si->GetPpid_implemented)
  44.     {
  45.         int p = GetPpid(si);
  46.         if(p != -1)
  47.             printf("ppid: %d\n",p);
  48.         else
  49.             printf("ppid: unknown\n");
  50.     }
  51.     if(si->GetPgrp_implemented)
  52.     {
  53.         int p = GetPgrp(si);
  54.         if(p != -1)
  55.             printf("pgrp: %d\n",p);
  56.         else
  57.             printf("pgrp: unknown\n");
  58.     }
  59.  
  60.  
  61.     /* If si->which_implemented is 0, then GetNice() and SetNice() are not available */
  62.     /* We'll also make sure that the search methods we need have been implemented    */
  63.     if(si->which_implemented && (si->which_implemented & (WHICHF_PRIO_TASK | WHICH_PRIO_PROCESS)))
  64.     {
  65.         int nice;
  66.  
  67.         /* display the nice-value for this task */
  68.         nice=GetNice(si,WHICH_PRIO_TASK,0);
  69.         if(nice == -1)
  70.         {
  71.             if(si->errno)
  72.                 printf("GetNice() failed, errno: %d\n",si->errno);
  73.             else
  74.                 printf("nice: %d\n",nice);
  75.         }
  76.         else
  77.             printf("nice: %d\n",nice);
  78.  
  79.         /* set our nice-value to +5 */
  80.         if(SetNice(si,WHICH_PRIO_PROCESS,GetPid(si),5))
  81.             printf("SetNice() failed, errno: %d\n",si->errno);
  82.     }
  83.  
  84.  
  85.     /* Ask for notify and output load averages every second for 10 seconds. */
  86.  
  87.     if(si->loadavg_type != LOADAVG_NONE)
  88.     {
  89.         struct sysinfo_notify *not;
  90.         struct Message *msg;
  91.         short i;
  92.  
  93.         if(si->notify_msg_implemented && (not=AddNotify(si,TRUE, 10)))
  94.         {
  95.             printf("load averages (%d.%02d, %d.%02d, %d.%02d minutes):\n",
  96.                 si->loadavg_time1/60, si->loadavg_time1%60,
  97.                 si->loadavg_time2/60, si->loadavg_time2%60,
  98.                 si->loadavg_time3/60, si->loadavg_time3%60);
  99.  
  100.             for(i=0;i<10;i++)
  101.             {
  102.                 /* We'll get a message every second. There may be more than
  103.                  * one message in the port at once. */
  104.  
  105.                 Wait(1L<<not->notify_port->mp_SigBit);
  106.                 while(msg = GetMsg(not->notify_port))
  107.                 {
  108.                     struct loadaverage load;    /* This will be filled by GetLoadAverage() */
  109.  
  110.                     ReplyMsg(msg);
  111.  
  112.                     GetLoadAverage(si, &load);    /* Ask sysinfo.library for current load averages */
  113.  
  114.                     printf("load average:");
  115.  
  116.                     switch(si->loadavg_type)
  117.                     {
  118.                         case LOADAVG_FIXEDPNT:
  119.  
  120.                             /* Convert fixed point values to floating point values */
  121.  
  122.                             if(si->loadavg_time1)
  123.                                 printf(" %.2f",(float) load.loadaverage.lavg_fixed.load1 / (float) si->fscale);
  124.                             else
  125.                                 printf(" N/A");
  126.  
  127.                             if(si->loadavg_time2)
  128.                                 printf(" %.2f",(float) load.loadaverage.lavg_fixed.load2 / (float) si->fscale);
  129.                             else
  130.                                 printf(" N/A");
  131.  
  132.                             if(si->loadavg_time3)
  133.                                 printf(" %.2f\n",(float) load.loadaverage.lavg_fixed.load3 / (float) si->fscale);
  134.                             else
  135.                                 printf(" N/A\n");
  136.  
  137.                             break;
  138.                         case LOADAVG_FLOAT:
  139.  
  140.                             /* Load averages are already in floating point format */
  141.  
  142.                             if(si->loadavg_time1)
  143.                                 printf(" %.2f",load.loadaverage.lavg_float.load1);
  144.                             else
  145.                                 printf(" N/A");
  146.  
  147.                             if(si->loadavg_time2)
  148.                                 printf(" %.2f",load.loadaverage.lavg_float.load2);
  149.                             else
  150.                                 printf(" N/A");
  151.  
  152.                             if(si->loadavg_time3)
  153.                                 printf(" %.2f\n",load.loadaverage.lavg_float.load3);
  154.                             else
  155.                                 printf(" N/A\n");
  156.  
  157.                             break;
  158.                     }
  159.                 }
  160.             }
  161.             RemoveNotify(si,not);
  162.         }
  163.         else
  164.             printf("Can't use notification.\n");
  165.     }
  166.     else
  167.         printf("Load averages are not supported.\n");
  168.  
  169.  
  170.     /* output cpu usage values */
  171.     {
  172.         struct cpu_usage cu;
  173.  
  174.         GetCpuUsage(si,&cu);
  175.  
  176.         printf("cpu time:                 ");
  177.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  178.             printf("%d seconds used, %d seconds idle\n",cu.total_used_cputime, cu.total_elapsed_time - cu.total_used_cputime);
  179.         else
  180.             printf("N/A\n");
  181.  
  182.         printf("cpu usage:                ");
  183.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTAL_IMPLEMENTED)
  184.             printf("%.2f%%\n",((float) cu.total_used_cputime * 100.0) / (float) cu.total_elapsed_time);
  185.         else
  186.             printf("N/A\n");
  187.  
  188.         printf("current cpu usage:        ");
  189.         if(si->cpu_usage_implemented & CPU_USAGEF_LASTSEC_IMPLEMENTED)
  190.             printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  191.         else
  192.             printf("N/A\n");
  193.  
  194.         printf("recent cpu usage:         ");
  195.         if(si->cpu_usage_implemented & CPU_USAGEF_RECENT_IMPLEMENTED)
  196.             printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  197.         else
  198.             printf("N/A\n");
  199.  
  200.         printf("context switches:         ");
  201.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  202.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  203.         else
  204.             printf("N/A\n");
  205.  
  206.         printf("total context switches:   ");
  207.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  208.             printf("%d\n", cu.total_csw);
  209.         else
  210.             printf("N/A\n");
  211.  
  212.         printf("current context switches: ");
  213.         if(si->cpu_usage_implemented & CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  214.             printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  215.         else
  216.             printf("N/A\n");
  217.  
  218.         printf("current total csws:       ");
  219.         if(si->cpu_usage_implemented & CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  220.             printf("%d\n", cu.total_csw_lastsec);
  221.         else
  222.             printf("N/A\n");
  223.     }
  224.  
  225.  
  226.     /* output cpu usage values for this task */
  227.     {
  228.         struct task_cpu_usage cu;
  229.  
  230.         if(!GetTaskCpuUsage(si,&cu,0))
  231.         {
  232.             printf("This task:\n");
  233.  
  234.             printf("cpu time:                   ");
  235.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  236.                 printf("%d.%d seconds\n",cu.total_used_cputime / cu.total_used_time_hz, cu.total_used_cputime % cu.total_used_time_hz);
  237.             else
  238.                 printf("N/A\n");
  239.  
  240.             printf("cpu usage:                  ");
  241.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTAL_IMPLEMENTED)
  242.                 printf("%.2f%%\n",(((float) cu.total_used_cputime) / ((float) cu.total_used_time_hz) * 100.0) / (float) cu.total_elapsed_time);
  243.             else
  244.                 printf("N/A\n");
  245.  
  246.             printf("current cpu usage:          ");
  247.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_LASTSEC_IMPLEMENTED)
  248.                 printf("%.2f%%\n",((float) cu.used_cputime_lastsec * 100.0) / (float) cu.used_cputime_lastsec_hz);
  249.             else
  250.                 printf("N/A\n");
  251.  
  252.             printf("recent cpu usage:           ");
  253.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_RECENT_IMPLEMENTED)
  254.                 printf("%.2f%% (%d seconds)\n",((float) cu.recent_used_cputime * 100.0) / (float) cu.recent_used_cputime_hz, cu.recent_seconds);
  255.             else
  256.                 printf("N/A\n");
  257.  
  258.             printf("context switches:           ");
  259.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_IMPLEMENTED)
  260.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw, cu.voluntary_csw);
  261.             else
  262.                 printf("N/A\n");
  263.  
  264.             printf("total context switches:     ");
  265.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_IMPLEMENTED)
  266.                 printf("%d\n", cu.total_csw);
  267.             else
  268.                 printf("N/A\n");
  269.  
  270.             printf("context switches (ps):      ");
  271.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_IVVOCSW_LASTSEC_IMPLEMENTED)
  272.                 printf("%d involuntary, %d voluntary\n", cu.involuntary_csw_lastsec, cu.voluntary_csw_lastsec);
  273.             else
  274.                 printf("N/A\n");
  275.  
  276.             printf("total context switches (ps):");
  277.             if(si->task_cpu_usage_implemented & TASK_CPU_USAGEF_TOTALCSW_LASTSEC_IMPLEMENTED)
  278.                 printf("%d\n", cu.total_csw_lastsec);
  279.             else
  280.                 printf("N/A\n");
  281.         }
  282.         else
  283.             printf("Can't get CPU usage for this task.\n");
  284.     }
  285.  
  286.     if(si)
  287.         FreeSysinfo(si);
  288.  
  289.     if(SysinfoBase)
  290.         CloseLibrary(SysinfoBase);
  291.  
  292.     return(RETURN_OK);
  293. }
  294.